package com.example.boke.controller;

import com.example.boke.entity.Boke;
import com.example.boke.mapper.BokeMapper;
import com.example.boke.tool.LocalDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/detail/")
public class BokeController {
    @Autowired(required = false)
    BokeMapper mapper;

    @RequestMapping("send")
    public int send(Boke boke, MultipartFile picFile,HttpSession session) throws IOException {
//        User user= (User) session.getAttribute("user");
//        if (user==null){
//            return 2;//当前用户未登录
//        }

        String fileName=picFile.getOriginalFilename();//获得原始文件名
        String suffix=fileName.substring(fileName.lastIndexOf("."));//通过文件名获取后缀
        fileName = UUID.randomUUID()+suffix;
        //文件路径
        //设置路径
        String dirPath = LocalDir.dir() + "/boke";
        File dirFile=new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath=dirPath+"/"+fileName;
        picFile.transferTo(new File(filePath));
        boke.setUrl("/"+fileName);
//        boke.setAuthor(user.getNick());
//        boke.setUserId(user.getId());
        boke.setCreatde(new Date());
        mapper.insert(boke);

        return 1;//发表成功

    }
    @RequestMapping("delete")
    public void delete(int id){
//        String url = mapper.selectUrlById(id);
//        LocalDir toolDir=new LocalDir();
//        String dirPath=toolDir.getDir()+"/boke";
//        String filePath = dirPath+url;
//        new File(filePath).delete();z
        mapper.delete(id);

    }
}
